package com.zhiyueshufang.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.annotation.ColorRes
import androidx.core.content.ContextCompat
import androidx.viewpager2.widget.ViewPager2
import com.zhiyueshufang.R
import com.zhiyueshufang.adapter.ViewPagerFragmentStateAdapter
import com.zhiyueshufang.databinding.ActivityHomeBinding

class HomeActivity : BaseActivity(), View.OnClickListener {

    lateinit var binding: ActivityHomeBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityHomeBinding.inflate(layoutInflater)//谷歌推荐的ViewBinding
        setContentView(binding.root)

        initComponent()

        initViewPager()

        initListener()
    }

    private fun initComponent() {
        binding.rlHomeContainer.setPadding(0, statusBarHeight, 0, 0)//
        binding.rlColorBar.layoutParams.height = statusBarHeight//设置顶部栏高度
    }

    public fun setStatusBarColor(@ColorRes colorId: Int) {
        binding.rlColorBar.setBackgroundColor(
            ContextCompat.getColor(
                this,
                colorId
            )
        )//设置顶部栏颜色
    }

    /**
     * 初始化侦听
     */
    private fun initListener() {
        binding.rlIndex.setOnClickListener(this)
        binding.rlTask.setOnClickListener(this)
        binding.rlMine.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            binding.rlIndex.id ->
                binding.vpHomeContainer.currentItem = 0
            binding.rlTask.id ->
                binding.vpHomeContainer.currentItem = 1
            binding.rlMine.id ->
                binding.vpHomeContainer.currentItem = 2

        }
    }

    /**
     * 初始化ViewPager
     */
    private fun initViewPager() {
        var vp_container: ViewPager2 = findViewById(R.id.vp_home_container)
        var adapter = ViewPagerFragmentStateAdapter(this)
        vp_container.adapter = adapter

        /**
         * 匿名内部类
         */
        vp_container.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {

                //初始化颜色和字体
                setIconAndColor(position)
            }
        })
    }

    /**
     * 初始化颜色和字体
     */
    private fun setIconAndColor(i: Int) {
        binding.ivIcHome.setImageResource(R.mipmap.ic_home)
        binding.ivIcTask.setImageResource(R.mipmap.ic_task)
        binding.ivIcMine.setImageResource(R.mipmap.ic_mine)
        binding.tvIcHome.setTextColor(
            ContextCompat.getColor(
                this@HomeActivity,
                R.color.light_gray
            )
        )
        binding.tvIcTask.setTextColor(
            ContextCompat.getColor(
                this@HomeActivity,
                R.color.light_gray
            )
        )
        binding.tvIcMine.setTextColor(
            ContextCompat.getColor(
                this@HomeActivity,
                R.color.light_gray
            )
        )


        //根据获取到的位置，动态修改样式
        if (i == 0) {
            binding.ivIcHome.setImageResource(R.mipmap.ic_home_active)
            binding.tvIcHome.setTextColor(
                ContextCompat.getColor(
                    this@HomeActivity,
                    R.color.light_blue
                )
            )
        }
        if (i == 1) {
            binding.ivIcTask.setImageResource(R.mipmap.ic_task_active)
            binding.tvIcTask.setTextColor(
                ContextCompat.getColor(
                    this@HomeActivity,
                    R.color.light_blue
                )
            )
        }
        if (i == 2) {
            binding.ivIcMine.setImageResource(R.mipmap.ic_mine_active)
            binding.tvIcMine.setTextColor(
                ContextCompat.getColor(
                    this@HomeActivity,
                    R.color.light_blue
                )
            )
        }
    }

    /**
     * 返回事件
     */
    override fun onBackPressed() {
        val system_home_page_intent = Intent()
        system_home_page_intent.action = "android.intent.action.MAIN"
        system_home_page_intent.addCategory("android.intent.category.HOME")
        system_home_page_intent.addCategory("android.intent.category.DEFAULT")
        system_home_page_intent.addCategory("android.intent.category.MONKEY")
        startActivity(system_home_page_intent)
    }
}